<?php 
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

defined('THINK_PATH') or exit();

Class SessionMemcache
{
	private $mem;
	private $expire;
	
	public function execute()
	{
		session_set_save_handler(
			array(&$this,'open'), 
			array(&$this,'close'), 
			array(&$this,'read'), 
			array(&$this,'write'), 
			array(&$this,'destroy'), 
			array(&$this,'gc')
			);
	}

	public function open($path,$name)
	{
		$this->expire = C('SESSION_EXPIRE') ? C('SESSION_EXPIRE') :ini_get('session.gc_maxlifetime');
		$this->mem = new Memcache;
		return $this->mem->connect(C('MEMCACHE_HOST'), C('MEMCACHE_PORT'));
	}

	public function close()
	{
		return $this->mem->close();
	}

	public function read($id)
	{
		$id = C('SESSION_PREFIX').$id;
		$data = $this->mem->get($id);
		return $data ? $data :'';
	}

	public function write($id,$data)
	{
		$id = C('SESSION_PREFIX').$id;
		return $this->mem->set($id,$data,0,$this->expire);
	}

	public function destroy($id)
	{
		$id = C('SESSION_PREFIX').$id;
		return $this->mem->delete($id);
	}

	public function gc()
	{
		return true;
	}
}
